//
//  CYPersonReDtVC.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 16/1/20.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYPersonReDtVC.h"
#import "CYURL.h"
#import "CYPersonReModel.h"
#import "UIImageView+WebCache.h"
#import "LEBarButtonItem.h"
#import "CYLoginHttpTool.h"
#import "CYUser.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"
#import "CYHttpTool.h"
#import "CYJsonInfo.h"
#import "MWPhoto.h"
#import "LEPhotoBrowserTool.h"

@interface CYPersonReDtVC ()
{
    NSMutableArray *_dataArray;//数据源
    NSMutableArray *_dataArray1;
    
}


@property (nonatomic, strong) NSString *qqstr;
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UILabel *timeLab;
@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet UILabel *sexLab;
@property (weak, nonatomic) IBOutlet UILabel *phoneLab;
@property (weak, nonatomic) IBOutlet UILabel *idCardLab;
@property (weak, nonatomic) IBOutlet UILabel *addressLab;
@property (weak, nonatomic) IBOutlet UILabel *qqLab;
@property (weak, nonatomic) IBOutlet UITextView *detailTView;
@property (weak, nonatomic) IBOutlet UIView *photoView;
@property (nonatomic, strong) NSMutableArray *picAddress;

@end

@implementation CYPersonReDtVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self createbtn];
     _picAddress = [[NSMutableArray alloc]init];
     _titleLab.text = _cypermodel.title;
     _timeLab.text = _cypermodel.createTime;
     _nameLab.text =[NSString stringWithFormat:@"姓名：%@",_cypermodel.name];
     if (_cypermodel.sex.intValue == 1) {
          _sexLab.text = @"性别：男";
     }else{
          _sexLab.text = @"性别：女";
     }
     _phoneLab.text = [NSString stringWithFormat:@"电话号码：%@",_cypermodel.phone];
     _idCardLab.text = [NSString stringWithFormat:@"身份证号：%@",_cypermodel.certificateNo];
     _addressLab.text = [NSString stringWithFormat:@"所在地区：%@-%@-%@",_cypermodel.province,_cypermodel.city,_cypermodel.area];
     if (_cypermodel.qq) {
          _qqLab.text = [NSString stringWithFormat:@"QQ：%@",_cypermodel.qq];
          
     }else{
          _qqLab.text = @"QQ:--";
          
     }
     _detailTView.text = _cypermodel.content;
     [self setimagecerIDView];

     

    
    // Do any additional setup after loading the view from its nib.
}

- (void)setimagecerIDView{
     
     // image 前缀  http://image.credit-manage.com  http://www.credit-manage.com/
     NSString *preFixString = @"http://image.credit-manage.com/";
     // 屏幕的宽度 (也是承载imageView的view的宽度)
     CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
     // image各个边界的间隔
     CGFloat space = 10.f;
     // imageview的width (设定为5张)
     CGFloat imgWidth = (screenWidth - space * 4) / 3;
     // imageview的height
     CGFloat imgHeight = _photoView.frame.size.height - space * 2;
     // 由于是xib + 手码, 代码获取xib控件的frame 必须在主线程中获取
     dispatch_async(dispatch_get_main_queue(), ^{
          [_picAddress addObject:_cypermodel.photo];
                              for (int i = 0; i < _picAddress.count; i++ ) {
                                             // image的url string
                                             NSString *imgUrlString = [_picAddress objectAtIndex:i];
                                                                 imgUrlString = [preFixString stringByAppendingString:imgUrlString];
                                                                 // 创建imageView
                                                                 UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(space + (imgWidth + space) * i, space, imgWidth, imgHeight)];
                                                                 imgView.backgroundColor = [UIColor whiteColor];
                                                                 // 此处你可使用SDWebImage来加载
                                                                 [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrlString]];
                                                                 [_photoView addSubview:imgView];
                                                            }
                                                            if (_picAddress.count){
                                                                 UITapGestureRecognizer *tag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoBrowser:)];
                                                                 [_photoView addGestureRecognizer:tag];
                                                            }
          

          });
     
     
     
}
/**
 *  图片浏览器
 */

- (void)photoBrowser:(UITapGestureRecognizer *)tag {
     NSArray *urls = _picAddress;
     MWPhoto *photo = nil;
     NSMutableArray *photos = [NSMutableArray array];
     for (NSString *url in urls) {
          photo = [MWPhoto photoWithURL:CYURLWithService(url)];
          //        photo.caption = @"title";
          [photos addObject:photo];
     }
     [LEPhotoBrowserTool showPhotoBrowserWithController:self photos:photos];
}


-(void)createbtn{
    
    LEBarButtonItem *itema = [[LEBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-shanchu (2)"] andSelImage:nil andTitle:@"删除" target:self action:@selector(moreBtn)];
    self.navigationItem.rightBarButtonItem = itema;
    
}

-(void)moreBtn{
    
    NSString *strid = _cypermodel.createid;
    NSString *type = _cypermodel.isExamine;
    CYLog(@"strid = %@",strid);
    CYLog(@"type = %@",type);
    CYUser *user = [CYLoginHttpTool userData].user;
    
    if (type.intValue == 1 ||type.intValue == -1 ||type.intValue == 2) {
        [CYHttpTool postWithURL:[CYURL shared].deleteNetworkCollectRecord params:@{@"userId":user.userId,@"token":user.token,@"id":strid} success:^(id json) {
            [self.navigationController popViewControllerAnimated:YES];
            
        } failure:^(NSError *error) {
            
        }];
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"你无权操作待审核的发布信息" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        
        [alertView show];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
